<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.0/axios.min.js"></script>
</head>
<body>
<h1>上传单个文件</h1>
<form action="upload.do" method="post" enctype="multipart/form-data">
    <input name="file" type="file">
    <button>上传</button>
</form>

<h1>图片上传</h1>
<form id="ff" action="uploadImg.do" method="post" enctype="multipart/form-data">
    <input name="file" type="file" id="file" onchange="uploadImg()" style="display: none">
    <!--<button type="button" onclick="uploadImg()">上传</button>-->
</form>
<img id="img" height="100px" width="100px" onclick="file.click()" style="border: 1px blue solid">
<script>
    function uploadImg(){
        var fd = new FormData(ff);
        axios.post("uploadImg.do",fd).then(res=>{
            if(res.data.code == 1){
                img.src = res.data.data;
            } else {
                alert(res.data.msg);
            }
        }).catch(e=>{
            alert(e.message);
            console.info(e);
        })
    }
</script>

</body>
</html>